<?php
namespace app\admin\middleware;
use think\facade\Config;
use think\facade\Request;
use Auth\Auth;

/**
 * 争个后台模块接口请求处理中间件
 */
class AdminGroupCheck
{
	/**
	 * handle  重写处理请求对象的操作函数
	 * @param  object Request $request 请求对象
	 * @param  object \Closure $next 响应对象
	 * @return array 错误返回的信息
	 *         code 返回码
	 *         msg 返回信息
	 *         data 返回数据
	 * @return  object 响应对象
	 */
	public function handle($request, \Closure $next)
	{
		// 访问入口信息
		$controller = strtolower(Request::controller());
		$function = strtolower(Request::action());
		// 对请求接口目标路径进行处理如果带有扩展名则将扩展名消除
		if (preg_match("/^[\w][\w]{0,31}[\.][a-zA-Z]{1,10}$/", $function)) $function = explode('.',$function)[0];
		else $function = $function;
		// 获取配置
		$config = Config::get($controller.'.');
		// 对 request 访问进行检测
		if (true === $config['request'])
		{
			$res = self::requestCheck($function,$config['request_switch']);
			if (true !== $res) exit(json_encode(['code'=>1,'msg'=>'Requert_Disable','data'=>'']) );
		}
		// 格式化与处理前台参数
		$request = self::rulesParamDispose($function,$request);
		return $next($request);
	}

	protected static function requestCheck($scene,$config)
	{	
		$res = '';
		$request = key_exists($scene,$config);
		// 请求是否存在
		if (true === $request)
		{
			switch ($scene)
			{
				// 页面地址请求验证
				case 'index':
					if (true === $config[$scene]) { if (Request::isGet()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'list':
					if (true === $config[$scene]) { if (Request::isGet()) $res = '0'; else $res = '1'; } 
					else $res = '2'; break;
				case 'add': 
					if (true === $config[$scene]) { if (Request::isGet()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'edit': 
					if (true === $config[$scene]) { if (Request::isGet()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'listgroup': 
					if (true === $config[$scene]) { if (Request::isPost()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'rulegroup': 
					if (true === $config[$scene]) { if (Request::isPost()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'addgroup': 
					if (true === $config[$scene]) { if (Request::isPost()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'editgroup': 
					if (true === $config[$scene]) { if (Request::isPost()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'getgroup': 
					if (true === $config[$scene]) { if (Request::isPost()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;
				case 'delgroup': 
					if (true === $config[$scene]) { if (Request::isPost()) $res = '0'; else $res = '1'; }
					else $res = '2'; break;

			}
		}
		else
		{
			// 架构时候 指向数字1 方便调试报错
			$res = '3';//请求不存在处理 期望跳转到404？
		}
		// 结果反馈
		switch ($res)
		{
			case '0': return true; break;// 请求验证通过状态
			case '1': return false; break;// 请求验证失败状态
			case '2': return true; break;// 请求验证被关闭状态 
			case '3': return true; break;// 不存在的接口请求状态 
		}
	}

	protected static function rulesParamDispose ($scene,$request)
	{
		switch ($scene)
		{
			case 'addgroup':
				$request->title = Request::param('grouptitle');
				$request->status = Request::param('switch');
				$request->note = Request::param('introduce');
				$request->rules = Request::param('rules');
			break;
			case 'editgroup':
				$request->id = Request::param('id');
				$request->title = Request::param('name');
				$request->status = Request::param('switch');
				$request->note = Request::param('introduce');
				$request->rules = Request::param('rules');
			break;
			case 'getgroup':
				$request->id = Request::param('id');
			break;
			case 'delgroup':
				$request->id = Request::param('id');
			break;
		}
		return $request;
	}
}
